/*
 * Copyright (c) 2017, Li Bo/Alex All rights reserved.
 * 
 * http://blog.sina.com.cn/alex4java
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.jlogger.manager;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.hyts.jlogger.config.Config;


/**
 * @ClassName: LoggerHandlerManager
 * @Description: LoggerHandlerManager日志句柄操作处理管理器<br>
 * 				  此管理器 为了方便用户在程序中控制对应的日志输出器状况<br>
 * 			            时时刻刻调整对应的Handler类操作独有的特性<br>
 * @author: 李博/Alex
 * @date: 2017-3-7 下午6:01:02
 * @email:  alexlibochn@china-pui.com.cn
 */
public final class LoggerHandler {
	
	
	public synchronized Logger addConsoleHandler(Level level) throws SecurityException, UnsupportedEncodingException{
		return addConsoleHandler(null,level);
	}
	
	/**
	 * @Title: addConsoleHandler
	 * @Description: 增添对应的ConsoleHandler的输出器操作
	 * @param handler
	 * @return: Logger
	 * @throws UnsupportedEncodingException 
	 * @throws SecurityException 
	 */
	public synchronized Logger addConsoleHandler(Logger logger,Level level) throws SecurityException, UnsupportedEncodingException{
		if(logger == null)
			logger = LoggerManager.getLogger("");
		ConsoleHandler consoleHandler = new ConsoleHandler();
		consoleHandler.setEncoding(Config.CHARSET_ENCODE);
		consoleHandler.setLevel(level == null ? Level.INFO : level);
		logger.addHandler(consoleHandler);
		return logger;
	}
	
	/**
	 * @Title: addConsoleHandler
	 * @Description:  增添对应的ConsoleHandler的输出器操作
	 * @param logger
	 * @param level
	 * @param formatter
	 * @return
	 * @throws SecurityException
	 * @throws UnsupportedEncodingException
	 * @return: Logger
	 */
	public synchronized Logger addConsoleHandler(Logger logger,Level level,Formatter formatter) throws SecurityException, UnsupportedEncodingException{
		if(logger == null)
			logger = LoggerManager.getLogger("");
		ConsoleHandler consoleHandler = new ConsoleHandler();
		consoleHandler.setEncoding(Config.CHARSET_ENCODE);
		consoleHandler.setLevel(level == null ? Level.INFO : level);
		consoleHandler.setFormatter(formatter);
		logger.addHandler(consoleHandler);
		return logger;
	}
	
	/**
	 * @Title: addConsoleHandler
	 * @Description: 增添对应的ConsoleHandler的输出器操作
	 * @param logger
	 * @param level
	 * @param filter
	 * @return
	 * @throws SecurityException
	 * @throws UnsupportedEncodingException
	 * @return: Logger
	 */
	public synchronized Logger addConsoleHandler(Logger logger,Level level,Filter filter) throws SecurityException, UnsupportedEncodingException{
		if(logger == null)
			logger = LoggerManager.getLogger("");
		ConsoleHandler consoleHandler = new ConsoleHandler();
		consoleHandler.setEncoding(Config.CHARSET_ENCODE);
		consoleHandler.setLevel(level == null ? Level.INFO : level);
		consoleHandler.setFilter(filter);
		logger.addHandler(consoleHandler);
		return logger;
	}
	
	/**
	 * @Title: addFileHandler
	 * @Description: 增添对应的FileHandler的输出器操作
	 * @param level
	 * @return
	 * @throws SecurityException
	 * @throws IOException
	 * @return: Logger
	 */
	public Logger addFileHandler(Level level) throws SecurityException, IOException{
		return addFileHandler(level);
	}
	
	/**
	 * @Title: addFileHandler
	 * @Description: 增添对应的FileHandler的输出器操作
	 * @param handler
	 * @return: Logger
	 * @throws IOException 
	 * @throws SecurityException 
	 */
	public Logger addFileHandler(Logger logger,Level level) throws SecurityException, IOException{
		if(logger == null)
			logger = LoggerManager.getLogger("");
		FileHandler fileHandler = new FileHandler();
		fileHandler.setEncoding(Config.CHARSET_ENCODE);
		logger.addHandler(fileHandler);
		return logger;

	}
	
	/**
	 * @Title: addFileHandler
	 * @Description: 增添对应的FileHandler的输出器操作
	 * @param logger
	 * @param level
	 * @param formatter
	 * @return
	 * @throws SecurityException
	 * @throws IOException
	 * @return: Logger
	 */
	public Logger addFileHandler(Logger logger,Level level,Formatter formatter) throws SecurityException, IOException{
		if(logger == null)
			logger = LoggerManager.getLogger("");
		FileHandler fileHandler = new FileHandler();
		fileHandler.setEncoding(Config.CHARSET_ENCODE);
		fileHandler.setFormatter(formatter);
		logger.addHandler(fileHandler);
		return logger;
	}
	
	/**
	 * @Title: addFileHandler
	 * @Description: 增添对应的FileHandler的输出器操作
	 * @param logger
	 * @param level
	 * @param filter
	 * @return
	 * @throws SecurityException
	 * @throws IOException
	 * @return: Logger
	 */
	public Logger addFileHandler(Logger logger,Level level,Filter filter) throws SecurityException, IOException{
		if(logger == null)
			logger = LoggerManager.getLogger("");
		FileHandler fileHandler = new FileHandler();
		fileHandler.setEncoding(Config.CHARSET_ENCODE);
		fileHandler.setFilter(filter);
		logger.addHandler(fileHandler);
		return logger;
	}
	
	/**
	 * @Title: addSocketHandler
	 * @Description: 增添对应的SocketHandler的输出器操作
	 * @param handler
	 * @return: Logger
	 */
	public Logger addSocketHandler(FileHandler handler,Logger logger){
		return null;
	}
}
